package com.hzc.springchan.util;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密
 * @author chen990627
 * @date 2022/09/12
 */

public class MD5Utils {

    public static String encodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {

        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String newStr = base64Encoder.encode(md5.digest(str.getBytes("utf-8")));
        return newStr;
    }

    public static boolean checkPassWord(String newPassWord, String oldPassWord) throws NoSuchAlgorithmException, UnsupportedEncodingException{

        if (encodeByMd5(newPassWord).equals(oldPassWord)){
            return true;
        }else {
            return false;
        }
    }
}
